package com.myTimeUtils.android.TimeTracker;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Button;
import android.util.Log;
import android.view.View;
import com.myTimeUtils.android.TimeTracker.Schedule.*;
import com.myTimeUtils.android.TimeTracker.System.*;
import com.myTimeUtils.android.TimeTracker.View.*;
/**
 * Initializes the android application.
 *
 * @author Joseph Walker
 */
public class TimeTracker extends Activity implements UpdatableView{
	private boolean active;
	private ScheduleInfo info;
	private Thread scheduleThread;
	private Handler ticks;
	//For testing
	private Button button;
	/** 
	 * Called when the activity is first created. 
	 *
	 * @param savedInstanceState
	 */
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		this.info = new ScheduleInfo(this);
		this.ticks = new Handler(){
			public void handleMessage(Message message){
			        Log.d("Handler", "Message Recieved"); 
				info.notifyObserver();
			}
		};
		//Testing
		button = new Button(this);
		updateView("init");
		//End Testing
		this.setContentView(button);
	}
	/**
	 * Starts the threads
	 */
	public void onStart(){
		super.onStart();
		scheduleThread = new Thread( new Runnable(){
			public void run(){
				try{
					while(active){
						Thread.sleep(1000);
						Log.d("Thread", "Thread wakes up"); 
						ticks.sendMessage(ticks.obtainMessage());
					}
				}catch(Exception e){}	
			}
		}); 
		this.active = true;
		scheduleThread.start();
	}
	/**
	 * Stops the threads.
	 */
	public void onStop(){
		super.onStop();
		this.active = false;
	}
	/**
	 * Updates the display to the provided string.
	 *
	 * @param message	The message to display
	 */
	public void updateView(String message){
		Log.d("Process", "update called in TimeTracker"); 
		button.setText(message);
	}
}
